package org.springblade.tms.module.transportorder.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.util.Strings;
import org.springblade.common.utils.EnumUtils;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.tms.global.enums.TankTypeEnum;
import org.springblade.tms.global.enums.TorTypeEnum;
import org.springblade.tms.global.enums.WaybillStatusEnum;
import org.springblade.tms.module.demandextrainfo.base.DemandExtraService;
import org.springblade.tms.module.demandextrainfo.entity.TmDemandExtraInfo;
import org.springblade.tms.module.demandextrainfo.vo.TmDemandExtraInfoVO;
import org.springblade.tms.module.exceptioncosttype.entity.TMExceptionCostType;
import org.springblade.tms.module.exceptioncosttype.service.IExceptionCostTypeService;
import org.springblade.tms.module.exceptioncosttype.vo.TMExceptionCostTypeVO;
import org.springblade.tms.module.exceptioncosttype.wrapper.ExceptionCostTypeWrapper;
import org.springblade.tms.module.signedinfo.dto.TMSignedInfoDTO;
import org.springblade.tms.module.transportorder.base.TransportListService;
import org.springblade.tms.module.transportorder.dto.TMTransportOrderDTO;
import org.springblade.tms.module.transportorder.dto.TransportOrderDTO;
import org.springblade.tms.module.transportorder.dto.WaybillStatusChangeDto;
import org.springblade.tms.module.transportorder.entity.TMTransportOrder;
import org.springblade.tms.module.transportorder.service.ITransportOrderService;
import org.springblade.tms.module.transportorder.vo.TMTransportOrderVO;
import org.springblade.tms.module.transportorder.vo.TransportOrderExcel;
import org.springblade.tms.module.transportorder.vo.TransportOrderVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.transportorder.controller
 * @className: TransportListController
 * @author: yr
 * @description: 运输单
 * @date: 2022/3/18 14:01
 */
@RestController
@RequestMapping("transportorder/TransportList")
@Api(value = "运输单-yr", tags = "运输单接口-yr")
public class TransportListController {
	@Resource
	private TransportListService transportListService;
	@Resource
	private IExceptionCostTypeService exceptionCostTypeService;
	@Resource
	private DemandExtraService demandExtraService;
	@Resource
	private ITransportOrderService transportOrderService;

	/**
	 * @param transportOrderDTO:
	 * @return R<IPage < TMTransportOrderVO>>
	 * @author yr
	 * @description 运输单列表查询
	 * @date 2022/3/16 14:18
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "运输单列表", notes = "传入transportOrder")
	public R<IPage<TMTransportOrderVO>> list(TransportOrderDTO transportOrderDTO, Query query) {
		return transportListService.list(transportOrderDTO, query);
	}

	/**
	 * @param id:
	 * @return null
	 * @author yr
	 * @description 运输单详情
	 * @date 2022/3/17 10:45
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "运输单详情", notes = "传入demandOrderId")
	public R<TransportOrderVo> detail(Long id) {
		return transportListService.detail(id);
	}

	/**
	 * @param waybillStatusChangeDto:
	 * @return null
	 * @author yr
	 * @description 运单状态变更
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/waybillStatusChange")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "运单状态变更  操作枚举：operating", notes = "传入waybillStatusChangeDto")
	public R<Boolean> waybillStatusChange(@RequestBody WaybillStatusChangeDto waybillStatusChangeDto) {
		return transportListService.waybillStatusChange(waybillStatusChangeDto);
	}

	/**
	 * @param signedInfoDTO:
	 * @return null
	 * @author yr
	 * @description 签收
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/signForReceipt")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "签收", notes = "传入signedInfoDTO")
	public R<Boolean> signForReceipt(@RequestBody TMSignedInfoDTO signedInfoDTO) {
		return transportListService.signForReceipt(signedInfoDTO);
	}

	/**
	 * @param transportOrderDTO:
	 * @return null
	 * @author yr
	 * @description 运单修改
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "运单修改", notes = "传入transportOrderDTO")
	public R update(@RequestBody TMTransportOrderDTO transportOrderDTO) {
		return transportListService.update(transportOrderDTO);
	}

	/**
	 * 分页 异常费用类型
	 */
	@GetMapping("/typesOfFee")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "费用类型", notes = "传入orderType")
	public List<TMExceptionCostTypeVO> typesOfFee(@ApiParam(value = "运单类型", required = true) @RequestParam String orderType) {
		List<TMExceptionCostType> exceptionCostTypeList = exceptionCostTypeService
			.lambdaQuery().eq(TMExceptionCostType::getOrderType, orderType).list();
		return ExceptionCostTypeWrapper.build().listVO(exceptionCostTypeList);
	}

	/**
	 * 查询运单轨迹URL
	 */
	@GetMapping("/queryTheTrackOfTheWaybill")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = " 查询运单轨迹URL", notes = "传入运单号")
	public R<String> queryTheTrackOfTheWaybill(@RequestParam String consigNo) {
		return transportListService.queryTheTrackOfTheWaybill(consigNo);
	}


	/**
	 * 地址管理URL
	 */
	@GetMapping("/addressManagement")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = " 地址管理URL")
	public R<String> addressManagement() {
		return transportListService.addressManagement();
	}

	/**
	 * 监控中心url
	 */
	@GetMapping("/monitoringCenter")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "监控中心url")
	public R<String> monitoringCenter() {
		return transportListService.monitoringCenter();
	}

	/**
	 * 电子路书url
	 */
	@GetMapping("/roadBook")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "电子路书url")
	public R<String> roadBook() {
		return transportListService.roadBook();
	}

	/**
	 * 补全信息
	 */
	@PostMapping("fillTransport")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "运单补充信息补全信息", notes = "id不用传")
	public R<Long> fillTransport(@RequestBody @Validated(TmDemandExtraInfo.FILL_INFO_Transport.class) TmDemandExtraInfoVO input) {
		return R.data(demandExtraService.fillTransportExtra(input));
	}

	/**
	 * @param id:
	 * @return null
	 * @author yr
	 * @description 修改运单异常拆单状态
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/updateStart")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改运单异常拆单状态")
	public R update(@RequestParam String id, @RequestParam Long start) {
		TMTransportOrder transportOrderServiceById = transportOrderService.getById(id);
		transportOrderServiceById.setAbnormalSpelling(BigDecimal.valueOf(start));
		return R.status(transportOrderService.updateById(transportOrderServiceById));
	}

	/**
	 * @param signedInfoDTO:
	 * @return null
	 * @author yr
	 * @description 修改签收信息
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/signForReceiptUpdate")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改签收信息", notes = "传入signedInfoDTO")
	public R<Boolean> signForReceiptUpdate(@RequestBody TMSignedInfoDTO signedInfoDTO) {
		return transportListService.signForReceiptUpdate(signedInfoDTO);
	}

	/**
	 * @param transportOrderDTO:
	 * @return R<IPage < TMTransportOrderVO>>
	 * @author yr
	 * @description 运输单导出
	 * @date 2022/3/16 14:18
	 */
	@GetMapping("/download")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "运输单导出", notes = "传入transportOrder")
	public void download(TransportOrderDTO transportOrderDTO, Query query, HttpServletResponse response) {
		query.setCurrent(-1);
		query.setSize(-1);
		List<TMTransportOrderVO> tmTransportOrderVOS = transportListService.list(transportOrderDTO, query).getData().getRecords();
        Map<String, List<TMTransportOrderVO>> map = tmTransportOrderVOS.stream().collect(Collectors.groupingBy(TMTransportOrderVO::getTransportNo));
        List<TransportOrderExcel> transportOrderExcels = BeanUtil.copy(tmTransportOrderVOS, TransportOrderExcel.class);
		transportOrderExcels.forEach(transportOrderExcel -> {
			transportOrderExcel.setTransportStatus(EnumUtils.getEnumByValue(WaybillStatusEnum.class, transportOrderExcel.getTransportStatus()).get().description());
			transportOrderExcel.setOrderType(EnumUtils.getEnumByValue(TorTypeEnum.class, transportOrderExcel.getOrderType()).get().description());
			if (Strings.isNotBlank(transportOrderExcel.getTankType())) {
				transportOrderExcel.setTankType(EnumUtils.getEnumByValue(TankTypeEnum.class, transportOrderExcel.getTankType()).get().description());
			}
            Date createTime = map.get(transportOrderExcel.getTransportNo()).get(0).getCreateTime();
            transportOrderExcel.setCreateTime(LocalDateTimeUtil.of(createTime));
		});
		ExcelUtil.export(response, "TM门户运输单", "TM门户运输单", transportOrderExcels, TransportOrderExcel.class);
	}
}
